<template>
        <view class="content">
            <!-- #ifdef MP-WEIXIN -->
            <block v-if="sid=='qrcode'">
                <top-nav :inner-title="title" :navTip="navTip" is-show-back="false" is-show-home="true" is-fixed="true" :navH="navH" :n="n" is-white="bg-green" opacity="1"></top-nav>
            </block>
            <block v-else>
                <top-nav :inner-title="title" :navTip="navTip" is-show-back="true" is-show-home="false"is-fixed="true" :navH="navH" :n="n" is-white="bg-green" opacity="1"></top-nav>
            </block>
            
            <view :style="{height:navH + 30 + 'px'}"></view>
            <!-- #endif -->  
            <view v-for="(item,index) in storeList" :key="index" class="shop-list-item">
                  <view class="shop-info-left" @click='openStore(item.id)'>
                      <block v-if='item.disable && item.reserve_status != 1'>
                          <view class='close'></view>
                      </block>
                      <image class="shop-info-img" :src="item.shop_img"  lazy-load="true"></image>
                  </view>
                  <view class="shop-info-detail">
                      <view @click='openStore(item.id)'>
                          <view class="shop-info-detail-name ellipsis">
                            {{item.shop_name}}
                          </view>
                          <view class="shop-info-detail-middle" v-if=" item.reserve_status == 1 && item.disable != 0 ">
                              <view class="reser_icon">接受预定中</view>
                          </view>
                          <view class="shop-info-detail-middle">
                            <view class="shop-info-detail-sales">月销{{item.month_order}}笔</view>
                            <view class="shop-info-detail-send" v-if="item.send_price > 0">省{{item.send_price}}元配送费</view>
                          </view>
                      </view>
                      <view class="shop-tags" :class="item.arrowOpen ? 'arrowDown':'arrowUp'" @click='openDiscount(item.id)'> 
                          <view class="text_list">
                              <block v-for='(val,index ) in item.arrAll' :key='index'>
                                  <view class="sales_content" :class='index > 2 && item.arrowOpen ? "hide" : ""'>{{val}}</view>
                              </block>
                              <view class="green" v-if="item.open_oneself == '1'">支持自取</view>
                              <view class="green" v-if="item.open_oneself == '2'">仅支持自取</view>
                          </view>
                          <view class="arrow" v-if='item.arrAll.length>3'></view>
                      </view>
                  </view>
            </view>
            <view class="no-more" v-if="noMore">更多商家接入中，敬请期待！</view>
        </view>
</template>

<script>
    import uniFly from 'unifly';
	import request from '../../../common/request.js';
	import $api_base from '../../../api/api_base.js';
	import topNav from '@/template/topnav/index.vue';
    export default {
		components: { topNav },
		data() {
			return {
				storeList: [],
                add_time: '',
                hasMore: 0,
                isEmpty: true,
                pn: 1,
                pc: 30,
                navH: 0,
                n:0,
                title: '',
                navTip: '¥0配送费 / ¥0起购 / 优惠同享',
                noMore: false,
                schoolId: 0,
                sid:''
			}
		},
		onLoad(options){
            console.log(options);
            var self = this;
			this.navH     = getApp().globalData.navHeight;
            this.n        = getApp().globalData.n;
            // #ifdef MP-ALIPAY
            uni.setNavigationBarTitle({
              title: '自取列表'
            })
            // #endif
            
            if (options.q && options.q != 'undedined'){   //扫码进首页
                var q = decodeURIComponent(options.q);
                var qq = q.indexOf('?')
                if(qq>0){  // 存在参数
                    var qOptions = q.substring(qq+1, q.length);   //获取所有的参数
                    var qList=qOptions.split("&");
                    var string = {};
                    for(let i in qList){
                        let qList_v = qList[i].split('=');
                        string[qList_v[0]] = qList_v[1] 
                    }
                    this.schoolId = string.schoolId || getApp().globalData.schoolId;
                }else{
                    this.schoolId = getApp().globalData.schoolId || 0;
                }
                getApp().globalData.schoolId = this.schoolId
                this.sid = 'qrcode';
                self.beforeInitData();
            }else{
                this.schoolId = options.schoolId || getApp().globalData.schoolId;
                getApp().globalData.schoolId = this.schoolId
                self.loadList(false);
            }
        },
		onShow(optons){
			
		},
		methods:{
            beforeInitData: function () {
                if (!uni.getStorageSync('openid'))
                  this.initApp();
                else {
                    this.loadList(false);
                }
            },
            initApp: function() {
            	var self = this;
            	self.loginReg().then(function() {
            		//定位
            		var params = {
            			long_lat: getApp().longLat,
            			school_id: self.schoolId
            		};
            		$api_base.post($api_base.homeIndex,params).then(function(res) {
            			var data = res.data;
            			if (data.ok == 2) {         //新用户
            				getApp().globalData.schoolInfo = data.schoolInfo;
            				getApp().globalData.userInfo = data.userInfo;
            				getApp().globalData.schoolList = data.schoolList;
            				getApp().kfPhone = data.kfPhone;
            				getApp().isLogin = true;
            				uni.setStorageSync('uid', data.userInfo.uid);
                            self.loadList(false); 
            			} else if (data.ok == 1) {
            				getApp().globalData.schoolInfo = data.schoolInfo;
            				getApp().globalData.userInfo = data.userInfo;
            				getApp().globalData.schoolList = data.schoolList;
            				getApp().kfPhone = data.kfPhone;
            				getApp().isLogin = true; 
                            if(data.userInfo.bind_phone == 0){
                                getApp().globalData.isRegisted = false;
                            }else{
                                getApp().globalData.isRegisted = true;
                            }
            				self.loadList(false);
            				uni.setStorageSync('uid', data.userInfo.uid);
            			}
            		});
            	});
            },
            loadList: function (isAppend) {
                var pn = this.pn;
                var where = new Object();
                var self = this;
                where.pn = pn;
                where.school_id = this.schoolId;
                where.pc = this.pc;
                $api_base.post($api_base.getOneselfList,where,1).then(function(res) {
                    let data = res.data;
                    if (data.ok == 1) {
                        var isEmpty = data.list.length > 0 ? true : false;
                        self.isEmpty = isEmpty;
                        if (isEmpty){
                          let source1 = { arrowOpen: true };
                          data.list.forEach(function (v) {
                            let arrAll = [];
                            Object.assign(v, source1);
                            v.active_list.forEach(function (t) {
                              let arr = [];
                              arr = t.text.split(';');
                              let source2 = { text: arr };
                              Object.assign(t, source2);
                              arr.forEach((item) => {
                                arrAll.push(item);
                              });
                            })
                            let source3 = { arrAll : arrAll};
                            Object.assign(v, source3);
                          })
                          
                        }
                        if (isAppend) {
                            self.storeList = [...self.storeList, ...data.list];
                        } else {
                            self.storeList = data.list;

                            uni.showToast({
                                title: '附近共' + data.count + '个自取商家'
                            })
                        }
                    }
                });
            },
			openStore: function (e) {
                var storeId = e;
                uni.navigateTo({
                    url: '/pages/goods/goods?storeId=' + storeId + '&sid=oneself',
                })
            },
            openDiscount: function (e) {
              var storeId = e;
              this.storeList.find(item => {
                if (item.id == storeId){
                  item.arrowOpen = !item.arrowOpen;
                }
              })
              const that = this;
              that.storeList = [...that.storeList];
            },
			/**
			 * 页面相关事件处理函数--监听用户下拉动作
			 */
			onPullDownRefresh: function () {
				uni.stopPullDownRefresh();
			},
			/**
			 * 页面上拉触底事件的处理函数
			 */
			onReachBottom: function () {
			  var pn = this.pn;
			  var pc = this.pc;
			  var count = this.storeList.length;
			  if (pn * pc <= count) {
			      this.pn++;
			      this.loadList(true);
			  } else {
			    this.noMore = true;
			  }
			}
		}
	}
</script>
<style>
  page {
  background-color: #fff;
  }
</style>
<style lang="scss" scoped>
	@import 'index.scss';
</style>
